package cn.iocoder.yudao.module.pplan.controller.admin.shiftrecord;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pplan.controller.admin.shiftrecord.vo.ShiftRecordPageReqVO;
import cn.iocoder.yudao.module.pplan.controller.admin.shiftrecord.vo.ShiftRecordRespVO;
import cn.iocoder.yudao.module.pplan.controller.admin.shiftrecord.vo.ShiftRecordSaveReqVO;
import cn.iocoder.yudao.module.pplan.controller.admin.shiftrecord.vo.ShiftRecordSimpleRespVO;
import cn.iocoder.yudao.module.pplan.service.shiftrecord.ShiftRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 排班管理")
@RestController
@RequestMapping("/pplan/shift-record")
@Validated
public class ShiftRecordController {

    @Resource
    private ShiftRecordService shiftRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建排班管理")
    @PreAuthorize("@ss.hasPermission('pplan:shift-record:create')")
    public CommonResult<Long> createShiftRecord(@Valid @RequestBody ShiftRecordSaveReqVO createReqVO) {
        return success(shiftRecordService.createShiftRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新排班管理")
    @PreAuthorize("@ss.hasPermission('pplan:shift-record:update')")
    public CommonResult<Boolean> updateShiftRecord(@Valid @RequestBody ShiftRecordSaveReqVO updateReqVO) {
        shiftRecordService.updateShiftRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除排班管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pplan:shift-record:delete')")
    public CommonResult<Boolean> deleteShiftRecord(@RequestParam("id") Long id) {
        shiftRecordService.deleteShiftRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除排班管理")
                @PreAuthorize("@ss.hasPermission('pplan:shift-record:delete')")
    public CommonResult<Boolean> deleteShiftRecordList(@RequestParam("ids") List<Long> ids) {
        shiftRecordService.deleteShiftRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得排班管理")
    @Parameter(name = "id", description = "编号", required = true, example = "YYYY-MM-DD")
    @PreAuthorize("@ss.hasPermission('pplan:shift-record:query')")
    public CommonResult<ShiftRecordRespVO> getShiftRecord(@RequestParam("shiftDate") String shiftDate) {
        ShiftRecordRespVO shiftRecordRespVO = shiftRecordService.getShiftRecord(shiftDate);
        return success(shiftRecordRespVO);
    }

    @GetMapping("/simple/get")
    @Operation(summary = "获得排班管理")
    @Parameter(name = "id", description = "编号", required = true, example = "YYYY-MM-DD")
    @PreAuthorize("@ss.hasPermission('pplan:shift-record:query')")
    public CommonResult<List<ShiftRecordSimpleRespVO>> getShiftRecordSimple(@RequestParam("shiftDate") String shiftDate) {
        List<ShiftRecordSimpleRespVO> simpleRespVOS = shiftRecordService.getShiftRecordSimple(shiftDate);
        return success(simpleRespVOS);
    }

    @GetMapping("/page")
    @Operation(summary = "获得排班管理分页")
    @PreAuthorize("@ss.hasPermission('pplan:shift-record:query')")
    public CommonResult<PageResult<ShiftRecordRespVO>> getShiftRecordPage(@Valid ShiftRecordPageReqVO pageReqVO) {
        PageResult<ShiftRecordRespVO> pageResult = shiftRecordService.getShiftRecordPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出排班管理 Excel")
    @PreAuthorize("@ss.hasPermission('pplan:shift-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportShiftRecordExcel(@Valid ShiftRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ShiftRecordRespVO> list = shiftRecordService.getShiftRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "排班管理.xls", "数据", ShiftRecordRespVO.class, list);
    }

}